package com.apex.sdk.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;
import androidx.appcompat.widget.Toolbar;
import androidx.databinding.DataBindingUtil;

import com.apex.bluetooth.callback.GeneralCallback;
import com.apex.bluetooth.callback.MsgContentCallback;
import com.apex.bluetooth.core.EABleManager;
import com.apex.bluetooth.enumeration.EABleConnectState;
import com.apex.bluetooth.enumeration.QueryWatchInfoType;
import com.apex.sdk.R;
import com.apex.sdk.databinding.ActivityMsgBinding;
import com.apex.sdk.dialog.WaitingDialog;

import java.util.ArrayList;
import java.util.List;


public class MsgActivity extends AppCompatActivity {
    private final String TAG = this.getClass().getSimpleName();
    private WaitingDialog waitingDialog;

    List<String> msgList;
    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0x40) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                if (msgList != null && !msgList.isEmpty()) {

                    if (msgList.size() >= 4) {
                        activityMsgBinding.msg1.setText(msgList.get(0));
                        activityMsgBinding.msg2.setText(msgList.get(1));
                        activityMsgBinding.msg3.setText(msgList.get(2));
                        activityMsgBinding.msg4.setText(msgList.get(3));
                    } else {
                        if (msgList.size() == 3) {
                            activityMsgBinding.msg1.setText(msgList.get(0));
                            activityMsgBinding.msg2.setText(msgList.get(1));
                            activityMsgBinding.msg3.setText(msgList.get(2));
                        } else if (msgList.size() == 2) {
                            activityMsgBinding.msg1.setText(msgList.get(0));
                            activityMsgBinding.msg2.setText(msgList.get(1));
                        } else {
                            activityMsgBinding.msg1.setText(msgList.get(0));
                        }
                    }

                }
            } else if (msg.what == 0x41) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                Toast.makeText(MsgActivity.this, getString(R.string.failed_to_get_data), Toast.LENGTH_SHORT).show();
            } else if (msg.what == 0x42) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                activityMsgBinding.submit.setEnabled(true);
            } else if (msg.what == 0x43) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                activityMsgBinding.submit.setEnabled(true);
                Toast.makeText(MsgActivity.this, getString(R.string.modification_failed), Toast.LENGTH_SHORT).show();
            }
        }
    };
    ActivityMsgBinding activityMsgBinding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityMsgBinding = DataBindingUtil.setContentView(this, R.layout.activity_msg);
        activityMsgBinding.setLifecycleOwner(this);
        setContentView(activityMsgBinding.getRoot());
        activityMsgBinding.tool.setNavigationIcon(R.mipmap.exit_page);
        setSupportActionBar(activityMsgBinding.tool);
        getSupportActionBar().setTitle(null);
        activityMsgBinding.tool.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        EABleConnectState state = EABleManager.getInstance().getDeviceConnectState();
        if (state == EABleConnectState.STATE_CONNECTED) {
            if (waitingDialog == null) {
                waitingDialog = new WaitingDialog(MsgActivity.this);
            }

            EABleManager.getInstance().queryWatchInfo(QueryWatchInfoType.msgContent, new MsgContentCallback() {
                @Override
                public void msgInfo(List<String> list) {
                    msgList = list;
                    if (mHandler != null) {
                        mHandler.sendEmptyMessage(0x40);
                    }

                }

                @Override
                public void mutualFail(int i) {
                    if (mHandler != null) {
                        mHandler.sendEmptyMessage(0x41);
                    }
                }
            });
        }
        activityMsgBinding.submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activityMsgBinding.submit.setEnabled(false);
                String msg1 = activityMsgBinding.msg1.getText().toString();
                String msg2 = activityMsgBinding.msg2.getText().toString();
                String msg3 = activityMsgBinding.msg3.getText().toString();
                String msg4 = activityMsgBinding.msg4.getText().toString();
                if (TextUtils.isEmpty(msg1) && TextUtils.isEmpty(msg2) && TextUtils.isEmpty(msg3) && TextUtils.isEmpty(msg4)) {
                    activityMsgBinding.submit.setEnabled(true);
                    return;
                }
                if (msgList == null) {
                    msgList = new ArrayList<>();
                }
                msgList.clear();
                if (!TextUtils.isEmpty(msg1)) {
                    msgList.add(msg1);
                }
                if (!TextUtils.isEmpty(msg2)) {
                    msgList.add(msg2);
                }
                if (!TextUtils.isEmpty(msg3)) {
                    msgList.add(msg3);
                }
                if (!TextUtils.isEmpty(msg4)) {
                    msgList.add(msg4);
                }
                if (EABleManager.getInstance().getDeviceConnectState() == EABleConnectState.STATE_CONNECTED) {
                    EABleManager.getInstance().setMsgContent(msgList, new GeneralCallback() {
                        @Override
                        public void result(boolean b, int i) {
                            if (b) {
                                if (mHandler != null) {
                                    mHandler.sendEmptyMessage(0x42);
                                }
                            } else {
                                if (mHandler != null) {
                                    mHandler.sendEmptyMessage(0x43);
                                }
                            }
                        }

                        @Override
                        public void mutualFail(int i) {
                            if (mHandler != null) {
                                mHandler.sendEmptyMessage(0x43);
                            }
                        }
                    });
                }
            }
        });
    }

    @Override
    protected void onDestroy() {

        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
            mHandler = null;
        }
        if (waitingDialog != null) {
            waitingDialog.dismiss();
            waitingDialog = null;
        }

        super.onDestroy();
    }
}
